
import { assetManager, AssetManager, resources } from "cc";
import { ILoading, ILoadingDep } from "../../modules_common/loading/ILoading";

export class BundleMgr implements ILoadingDep {


    dep: ILoadingDep;
    next: ILoading[];
    bLoading: boolean;

    init() {

    }
    finit() {
    }

    loadedProgress: number = 0;
    readonly totalProgress: number = 3;


    resources: AssetManager.Bundle = resources; //登录界面 首屏幕等
    public: AssetManager.Bundle = null;
    jszip: AssetManager.Bundle = null;
    config: AssetManager.Bundle = null;

    load(): void {

        if (this.bLoading) {
            return;
        }
        this.bLoading = true;

        this.loadBundle("public", (bundle: AssetManager.Bundle) => {
            this.public = bundle;
        })
        this.loadBundle("jszip", (bundle: AssetManager.Bundle) => {
            this.jszip = bundle;
        })
        this.loadBundle("config", (bundle: AssetManager.Bundle) => {
            this.config = bundle;
        })
    }

    private loadBundle(name: string, cb: (bundle: AssetManager.Bundle) => void): void {
        assetManager.loadBundle(name, ((err: any, bundle: AssetManager.Bundle) => {
            if (err) {
                console.error(" load bundle error  .... ", err);
                return;
            }
            cb(bundle);
            this.loadedProgress++;
        }).bind(this))
    }


    checkCompleted(): boolean {
        const ret = this.loadedProgress >= this.totalProgress
        if (ret) {
            return true;
        }
        return false;
    }

    getProgress(): number {
        return this.loadedProgress / this.totalProgress;
    }

    getProgressDesc(): string {
        return "正在加载bundle" + this.getProgress();
    }

}
